<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "sms_code".
 *
 * @property integer $id
 * @property string $relation
 * @property integer $code_type
 * @property string $verify_code
 * @property string $expiration_time
 * @property integer $is_used
 * @property integer $error_count
 * @property string $create_time
 * @property string $ip
 */
class SmsCode extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sms_code';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['relation', 'code_type', 'verify_code', 'expiration_time', 'create_time', 'ip'], 'required'],
            [['relation', 'code_type', 'is_used', 'error_count'], 'integer'],
            [['expiration_time', 'create_time'], 'safe'],
            [['verify_code'], 'string', 'max' => 6],
            [['ip'], 'string', 'max' => 15],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'id',
            'relation' => '用户编号',
            'code_type' => '验证码类型',
            'verify_code' => '验证码',
            'expiration_time' => '过期时间',
            'is_used' => '是否使用',
            'error_count' => '验证码错误次数',
            'create_time' => '创建时间',
            'ip' => 'ip',
        ];
    }
     public static function itemAlias ($type, $code = NULL)
    {
        $_items = [
            'code_type' => [
                //''  =>'全部',
                '1' => "注册",              
                '2' => "余额支付",
                '3' => "找回交易密码",
                '4' => "找回登录密码",   
                '5' => "绑定银行卡验证码",
                
                             
            ],
            'is_used' => [
               // ''  =>'是否使用',
                '0' => "未使用",              
                '2' => "已使用",
                                
            ],
           
        ];

        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            //是否是新添加
            if($this->isNewRecord) {
                $this->create_time = date("Y-m-d H:i:s", time());
                //$this->update_at = date("Y-m-d H:i:s", time());
            }else{
                //$this->update_at = date("Y-m-d H:i:s", time());
            }
            return true;
        } else {
            return false;
        }
    } 

}
